#ifndef WSOCKCLIENTHELPER_H
#define WSOCKCLIENTHELPER_H

#include <QObject>
#include <QWebSocket>
#include <QByteArray>
#include <QJsonDocument>

class WSockResponse
{
public:
    int command;
    QString topic;
    QString reqid;
    int status;
    QString err;
    QString val;
};

class WSockClientHelper : public QObject
{
    Q_OBJECT
public:
    explicit WSockClientHelper(QObject *parent = nullptr);
    void connectToServer(QString url);                              //连接到服务器
    void publish(QString topic, QString message);                   //发布消息
    void subscribe(QString topic);                                  //订阅主题，结果在slotslotTextMessageReceived中返回，需要判断
    void callApi(QString topic, QString p);                         //调用接口，但结果在slotslotTextMessageReceived中返回，需要判断

signals:
    void onConnectServer();
    void onDisconnected();
    void onReceiveMessage(WSockResponse response);
    void onError();

private:
    QWebSocket* m_wsocket = nullptr;
    QMap<QString, QJsonDocument> m_requests;

private:
    void Send(QByteArray data, bool isText = true);                 //返送数据，isText表示是否为文本数据

private slots:
    void slotConnected();
    void slotDisconnected();
    void slotTextMessageReceived(QString message);
    void slotBinaryDataReceived(QByteArray data);
    void slotError(QAbstractSocket::SocketError);
};

#endif // WSOCKCLIENTHELPER_H
